package main

import (
	"context"
	"errors"
	"geekbang-go-homework/module2/http"
	"golang.org/x/sync/errgroup"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	group, ctx := errgroup.WithContext(context.Background())
	ports := []string{"8088", "8089"}
	for _, port := range ports {
		port := port
		group.Go(func() error {
			ser := http.NewServer(port)
			go func() {
				<-ctx.Done()
				ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
				defer cancel()
				ser.Stop(ctx)
			}()
			return ser.Start()
		})
	}
	group.Go(func() error {
		ch := make(chan os.Signal, 1)
		signal.Notify(ch, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT)
		for {
			select {
			case <-ch:
				return errors.New("server stop")
			}
		}
		return nil
	})
	group.Wait()
}
